<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table
              border=""
              :data="list"
            >
              <el-table-column
                align="center"
                type="index"
                label="序号"
                width="120"
              />
              <el-table-column
                align="center"
                label="角色名称"
                width="240"
                prop="name"
              />
              <el-table-column
                align="center"
                label="描述"
                prop="description"
              />

              <el-table-column
                label="操作"
                align="center"
              >

                <template slot-scope="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="assignPerm(row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="deleteRole(row.id)"
                  >删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="page.page"
                layout="prev,pager,next,total"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form
              label-width="120px"
              style="margin-top:50px"
            >
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyInfo.name"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyInfo.companyAddress"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyInfo.mailbox"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
    </div>

    <!-- 编辑弹出层 -->
    <el-dialog
      title="编辑弹层"
      :visible="showDialog"
      @close="btnCancel"
    >
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item
          label="角色名称"
          prop="name"
        >
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row
        slot="footer"
        type="flex"
        justify="center"
      >
        <el-col :span="6">
          <el-button
            size="small"
            @click="btnCancel"
          >取消</el-button>
          <el-button
            size="small"
            type="primary"
            @click="btnOK"
          >确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>

    <!-- 分配权限的弹出层 -->

    <!-- title 标题属性 :visible 设置弹出层是否显示 -->
    <el-dialog
      title="分配权限"
      :visible="showPermDialog"
      @close="btnPermCancel"
    >
      <!-- 权限是一颗树 -->
      <!-- 将数据绑定到组件上 -->
      <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
      <!-- id作为唯一标识 -->

      <!--
        data属性设置树型表格显示的数据
        props属性设置树型获取数据的映射
        default-expand-all属性自动展开树型数据的子节点
        show-checkbox属性设置树型数据单选框模式
        check-strictly属性设置父子是否不关联
        default-checked-keys属性设置默认勾选的数组
        node-key属性设置数据的唯一标识,用于设置勾选状态
       -->
      <el-tree
        ref="permTree"
        :data="permData"
        :props="defaultProps"
        :default-expand-all="true"
        :show-checkbox="true"
        :check-strictly="true"
        :default-checked-keys="selectCheck"
        node-key="id"
      />
      <!-- 确定 取消 -->
      <el-row
        slot="footer"
        type="flex"
        justify="center"
      >
        <el-col :span="6">
          <el-button
            type="primary"
            size="small"
            @click="btnPermOK"
          >确定</el-button>
          <el-button
            size="small"
            @click="btnPermCancel"
          >取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  deleteRole,
  updateRole,
  getRoleDetail,
  addRole,
  assignPerm
} from '@/api/setting'
import { tranListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      list: [],
      page: {
        page: 1,
        pagesize: 10,
        total: 0
      },
      companyInfo: {},
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空!', trigger: 'blur' }
        ]
      },
      showPermDialog: false /* 设置分配权限的弹出层显示 */,
      defaultProps: {
        label: 'name'
      } /* 设置树型结构的默认数据的映射关系 */,
      permData: [], // 专门用来接收权限数据 树形数据
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: null // 用来记录分配角色的id
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.list = rows
      this.page.total = total
    },
    changePage(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.companyId)
    },
    async deleteRole(id) {
      try {
        await this.$confirm('确认删除该角色吗')
        await deleteRole(id)
        this.getRoleList() // 重新加载数据
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    async editRole(id) {
      /* 通过id获取员工信息,回显数据 */
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },
    /* 点击确定更新角色信息 */
    async btnOK() {
      if (this.roleForm.id) {
        // 编辑

        /* 手动验证表单信息 */
        await this.$refs.roleForm.validate()
        // 验证成功,更新数据
        updateRole(this.roleForm)
      } else {
        // 新增

        // 调用新增请求函数
        await addRole(this.roleForm)
      }

      // 重新拉取数据
      this.getRoleList()
      this.$message.success('操作成功')
      // 关闭弹出层
      this.showDialog = false
    },

    // 取消按钮事件
    btnCancel() {
      // 清空
      this.roleForm = {
        name: '',
        description: ''
      }

      // 重置检验
      this.$refs.roleForm

      // 关闭弹出层
      this.showDialog = false
    },

    /* 分配权限按钮 */
    async assignPerm(id) {
      this.permData = tranListToTreeData(await getPermissionList(), '0') // 获取权限信息
      this.roleId = id // 将当前角色的id记录下来
      const { permIds } = await getRoleDetail(id) // 获取当前id的权限信息
      this.selectCheck = permIds // 将权限信息存储在勾选数组中
      this.showPermDialog = true
    },

    /* 分配权限的确定按钮 */
    async btnPermOK() {
      // 发送请求
      await assignPerm({
        permIds: this.$refs.permTree.getCheckedKeys(),
        id: this.roleId
      })
      // 弹出提示
      this.$message.success('分配权限成功! ')
      // 关闭弹出层
      this.showPermDialog = false
    },
    btnPermCancel() {
      // 重置回显数组
      this.selectCheck = []
      // 关闭弹出层
      this.showPermDialog = false
    }
  }
}
</script>

<style>
</style>
